#include "http.h"
#include <stdlib.h>
#include <cjson/cJSON.h>

char buf[2048];

int main()
{
    //设置请求头部字段
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "X-User-Token: o9M10gtneoIxRLk5UO13fUG7guYxnS7h");
    
    //设置API地址
    char* url = "https://v2.jinrishici.com/sentence";

    //发送请求消息
    size_t size = 0;
    char* response = get(url, headers, &size);
    curl_slist_free_all(headers);

    if (!response)
    {
        return EXIT_FAILURE;
    }

    //解析返回报文中的JSON字符串
    cJSON* json = cJSON_ParseWithLength(response, size);
    free(response);

    if (!json)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        return EXIT_FAILURE;
    }
    //printf(cJSON_Print(json));

    // 获取JSON根对象中data字段的值
    cJSON* data = cJSON_GetObjectItem(json, "data");
    if (data)
    {
        // 获取data子对象中content字段的值
        cJSON* content = cJSON_GetObjectItem(data, "content");
        if (content)
        {
            puts(content->valuestring);
        }
    }

    // 删除解析后对象占用的内存
    cJSON_Delete(json);

    return 0;
}
